<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 2020/12/12
 * Time: 2:11
 */
namespace app\admin\controller;
use app\common\model\UserModel;
use app\common\module\Wechat;
use think\Db;

class User extends Common
{
    public function lists(){
        $model = new UserModel();
        $keyWords = input('param.keywords','');
        $where = [];
        $user_class = input('param.user_class/d',0);
        if($user_class){
            $where[] = ['user_class','=',$user_class];
        }
        if($keyWords){
            $where[] = ['nickname','like',"%{$keyWords}%"];
        }

        $list = $model->where($where)->order('id','desc')->paginate(15,[]);
        $in = '';
        foreach ($list as $k=>$v1) {
            $in.=$v1['id'].',';
        }
        $fen = Db::name('order_appraise')
            ->where('accept_user_id','in',$in)->group('accept_user_id')
            ->field('round(sum(`receiving`)/count(*),2) as receiving,round(sum(`come`)/count(*),2) as come,
            round(sum(`service`)/count(*),2) as service,
            accept_user_id as user_id,
            round(sum(`come`+`receiving`+`service`)/count(*),2) as total,
            count(*) as total_appraise
            ')
            ->select();
        foreach ($list as $k=>$v1){
            foreach ($fen as $v2){
                if($v1['id'] == $v2['user_id']){
                    if($v1){
                        $list[$k] = array_merge($v1->toArray(),$v2);
                    }

                }
            }
        }
        return view('',['list'=>$list]);
    }
    public function share(){
        $where = [];
        $user_Id = input('param.user_id/d',0);
        if($user_Id){
            $where[] = ["parent_id","=",$user_Id];
        }
        $list = Db::name("users")->whereNotNull("parent_id")
            ->where($where)
            ->field("id as user_id,parent_id,nickname,headimgurl,create_time,true_name")
            ->order('id','desc')
            ->paginate(15,[]);
        $in =[];
        $userIsIn = [];

        $sum = "";
        $sum_weight ="";
        foreach ($list as $v){
            array_push($in,$v["parent_id"]);
            $userIsIn[] = $v["user_id"];

            $sum.="sum( user_id = ".$v["user_id"].") as u_".$v["user_id"]."," ."sum( if( user_id = ".$v["user_id"].",total_money,0))  as t_".$v["user_id"]."," ;
            $sum_weight.="sum( if( user_id = ".$v["user_id"].",number,0))  as w_".$v["user_id"]."," ;
        }
        array_push($in,4);
        $in =  array_unique($in);
        $newList = Db::name("users")->whereIn("id",$in)->field("id as share_id,nickname as share_nickname,headimgurl as share_headimgurl,true_name as share_true_name")->select();


        $sum = rtrim ( $sum ,  ",");
        $sum_weight = rtrim ( $sum_weight ,  ",");
        $address = Db::name("users_address")->whereIn("user_id",$userIsIn)->field("true_name,phone,type as address_type,user_id")->select();


        $order_list = Db::name("orders")
            ->whereIn("order_status","2,3")
            ->whereIn("user_id",$userIsIn)
            ->field($sum)->select();
        $weight_list = Db::name("order_detail")
            ->whereIn("user_id",$userIsIn)
            ->field($sum_weight)->select();


        $list1 = $list;
        $total = $list->total();
        $list = $list->items();
        foreach ($list as $k=>$v){
            foreach ($newList as $key=>$val){
                if($val["share_id"] == $v["parent_id"]){
                    $list[$k] = array_merge($list[$k],$val);
                }
            }


            foreach ($address as $val1){
                if($v["user_id"] == $val1["user_id"]){
//                    $list[$k]->true_name = $val1["true_name"];
                    $list[$k]["true_name"] = $val1["true_name"];
                    $list[$k]["phone"] =$val1["phone"];
                    $list[$k]["address_type"] =$val1["address_type"];
                }
            }

            //重量

            foreach ($weight_list as $v1){
                foreach ($v1 as $kk1=>$vvv1){
                    $ge = explode("_",$kk1);
                    if($ge[1] == $v["user_id"]){
                        if($ge[0] =="w"){
                            $list[$k]["order_weight"] = $vvv1;
                        }
                    }
                }
            }

            //订单
            foreach ($order_list as $vv){
                foreach ($vv as $kk=>$vvv){
                    $ge = explode("_",$kk);
                    if($ge[1] == $v["user_id"]){
                        if($ge[0] =="u"){
                            $list[$k]["order_count"] = $vvv;
                        }
                        if($ge[0] =="t"){
                            $list[$k]["order_money"] = $vvv;
                        }
                    }
                }
            }
        }
        return view('',['list1'=>$list,"total"=>$total,"list"=>$list1]);

    }
    public function lock(){
        $openid = input('param.openid/id',0);
        $status = input('param.is_lock','');
        if(!in_array($status,[0,1])){
            return package(10006,'类型错误');
        }
        $Userdata = redisHashGet('auth',$openid);
        if($Userdata){
            $Userdata['is_lock'] = $status;
            redisHashSet('auth',$Userdata['token'],$Userdata);
            redisHashSet('auth',$openid,$Userdata);
        }
        $result = Db::name('users')->where('openid',$openid)->update(['is_lock'=>$status]);
        return checkRes($result,'状态设定');

    }
    public function recycler_apply(){
        $where = [];
        $status = input('param.status/d',3);
        if($status!=3){
            $where[] = ['status','=',$status];
        }
        if(ADMINTYPE ==2){
            $where[] =  ['region_id','=',$this->adminInfo["region_id"]];
        }
        $list = Db::name('recycler_apply')
            ->where($where)
            ->field('id,true_name,headimgurl,nickname,id_card,phone,region_name,remarks,status,create_time,positive,back,user_id')
            ->order('id','desc')->paginate(15,[

            ]);
        return view('',['list'=>$list]);
    }
    public function checkStatus(){
        $id = input('param.id/d',0);
        $status = input('param.status/d',1);
        $wechat = new Wechat();
        if($status ==1){
            $openId =  Db::name('recycler_apply')->where('id',$id)->value('openid');
            $wechat->sendResultMsg($openId,'审核通过','回收员入驻审核','通过！');
            $result = Db::name('recycler_apply')->where('id',$id)->update(['status'=>$status]);
            return checkRes($result,'审核');
        }else if($status ==2){
            $openId =  Db::name('recycler_apply')->where('id',$id)->value('openid');
            $wechat->sendResultMsg($openId,'不通过','回收员入驻审核',input('param.reason/s',''));
            $result = Db::name('recycler_apply')->where('id',$id)->update(['status'=>$status,'reason'=>input('param.reason/s','')]);
            return checkRes($result,'审核');
        }
    }

    public function userOrderList(){
        $field = 'id,headimgurl,region_name,appointment_date,accept_user_name,appointment_time,address,order_number,address_type,type_name,true_name,phone,total_money,total_integral,weight_type,order_status,create_time';
        $info['searchWords'] = input('param.searchWords/s','');
        $info['order_status'] = input('param.order_status/d',5);
        $user_id = input('param.user_id',0);
        $where = [
            ['order_number|true_name|phone','like',"%{$info['searchWords']}%"],
            ['is_delete','=',0],

        ];
        if(!$user_id){
            $info['order_status'] = input('param.order_status/d',2);
            $where[]  =["user_id","=",input('param.share_id',0)];
        }else{
            $where[]  =  ['accept_user_id','=',$user_id];
        }




        $start_time = input('param.start_time','');
        $end_time = input('param.end_time','');
        if($start_time && $end_time){
            $where[] =  ['accept_time','between time',[$start_time.'00:00:00',$end_time."23:59:59"]];
        }
        if($info['order_status'] !=5){
            $where[] =  ['order_status','=',$info['order_status']];
        }
        $list = Db::name('orders')
            ->where($where)
            ->field($field)
            ->order('id','desc')
            ->paginate(10,false,[
                'query'=>array_merge($info,['start_time'=>$start_time,'end_time'=>$end_time,'user_id'=>$user_id])
            ]);
        $in =[];
        foreach ($list as $v){
            $in[] = $v['id'];
        }
        return view('',[
            'list'=>$list,
            'info'=>$info,
            'user_id'=>$user_id,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'this_time'=>date("Y-m-d"),
            'total_money'=> Db::name('orders')
                ->where($where)->sum('total_money'),
            'total_integral'=> Db::name('orders')
                ->where($where)->sum('total_integral'),
            'total_number'=>Db::name('order_detail')->whereIn('order_id',$in)->sum('number')
            ]);
    }
}